#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>

using namespace std; 

#define  N 32
#define  K 16

char num[N]; 
char rule[K][2]; 
int k; 

int bit_nr[N]; 

void dfs(int pos)
{
	char c; 
	int found, tgt=0; 
	char *p = &num[pos]; 
	int res = 1; 
	if (*p=='\0') {
		bit_nr[pos] = 1; 
		return; 
	}
	// tgt is the set of number which this bit could be
	tgt |= (1<<(*p-'0')); 
	do {
		found = 0; 
		for (int j=0; j<10; j++) { 
			// if this number is not reachable, skip it.
			if ((tgt&(1<<j))==0) {
				continue; 
			}
			c = j+'0'; 
			for (int i=0; i<k; i++) {
				// if the source-char of the rule is not the target, skip it
				if (rule[i][0]!=c)
					continue; 
				// if the source-char of the rule is already in the tgt, skip it
				if (tgt & (1<<(rule[i][1]-'0')))
					continue; 
				// found a new target-char could be reachable
				found = 1; 
				tgt |= 1<<(rule[i][1]-'0'); 
				res ++; 
			}
		}
	} while(found); 
	bit_nr[pos] = res; 
	dfs(pos+1); 
}


#define NB 100
void out_result(void)
{
	int big_int[NB], i, j;
	int last_bit = 0;  
	
	for (i=0; i<NB; i++) 
		big_int[i] = 0; 

	big_int[last_bit] = 1; 
	for (j=0; num[j]; j++) {
		for (i=0; i<=last_bit; i++) {
			big_int[i] *= bit_nr[j]; 
		}
		for (i=0; i<=last_bit; i++) {
			int a, b; 
			a = big_int[i]%10; 
			b = big_int[i]/10; 
			big_int[i] = a; 
			big_int[i+1] += b; 
		}
		if (big_int[i]!=0) {
			last_bit = i;
		} 
	}
	
	for (i=last_bit; i>=0; i--) {
		printf("%d", big_int[i]); 
	}
	printf("\n"); 
}

int main()
{
	int o, t; 
	freopen("p1037_5.in", "r", stdin); 
	scanf("%s %d", num, &k); 
	for (int i=0; i<k; i++) {
		scanf("%d %d", &o, &t); 
		rule[i][0] = '0' + o; 
		rule[i][1] = '0' + t; 
	}
	dfs(0); 
	out_result(); 
	return 0; 
}

